7 + 2
9
7 - 2
5
7 * 2
14
7 ** 2
49
7 ** 2
is $ 7 ^ 2 $
7 / 2
3.5
7 // 2
3
7 % 2
1
$\frac{7}{2} = 3r1$
7 // 2
gives us the $3$
7 % 2
gives us the $1$ (the remainder)
2 > 1
True
2 > 2
False
2 >= 2
True
4 == 4
True
4 != 6
True
This demonstrates the mapping pattern.
Map is a mathematical term that means going from one value to a corresponding value.
This is also referred to as the transform pattern.
%%file for_class/transform_a_list.py
def make_bigger(numbers):
bigger_numbers = []
for number in numbers:
bigger = number * 2
bigger_numbers.append(bigger)
return bigger_numbers
if __name__ == '__main__':
a_few_ints = [1, 2, 3, 4, 5, 6, 7, 8]
bigger = make_bigger(a_few_ints)
print(a_few_ints)
print(bigger)
smaller_numbers.py
¶Write a function that creates a new list where each item has been divided by 2.
You can pass lists to functions and return lists from functions.
This demonstrates the filter pattern.
A new collection is created with certain items filtered out.
This demonstrates the selection pattern.
A single item is selected from a collection.
This demonstrates the accumulator pattern.
total
accumulates the values in the collection.